package com.car.advertising.util;

import android.text.TextUtils;
import android.widget.EditText;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatUtil {
    /**
     * 距离格式转换 大于1000m单位为km 小于1000m单位为m
     */

    public static String formatDistance(String distance) {
        double dis = Double.parseDouble(distance);
        if (dis == -1) {
            return "";
        }
        if (dis < 1000) {
            DecimalFormat decimalFormat1 = new DecimalFormat("0");
            return decimalFormat1.format(dis) + "m";
        } else {
            DecimalFormat decimalFormat2 = new DecimalFormat("0.0");
            return decimalFormat2.format(dis / 1000) + "km";
        }

    }

    /**
     * 金额格式转换 大于1000m单位为km 小于1000m单位为m
     */

    public static String formatAmount(int lenth, String amount) {
        DecimalFormat decimalFormat = null;
        float a = Float.parseFloat(amount);
        switch (lenth) {
            case 0:
                decimalFormat = new DecimalFormat("0");
                break;
            case 1:
                decimalFormat = new DecimalFormat("0.0");
                break;
            case 2:
                decimalFormat = new DecimalFormat("0.00");
                break;
        }

        return decimalFormat.format(a) + "";

    }

    /**
     * 距离格式转换 小于1分钟，显示x秒 大于1分钟小于1小时，显示x分钟 大于1小时，显示x小时x分钟
     */
    public static String formatTime(String second) {
        double time1 = Double.parseDouble(second);
        if (time1 < 60) {// 显示x秒
            return time1 + "秒";
        } else if (time1 >= 60 && time1 < 3600) {
            int minute = (int) (time1 / 60);
            return minute + "分钟";
        } else {
            int hour = (int) (time1 / 3600);
            int minute = (int) ((time1 - hour * 3600) / 60);
            return hour + "小时" + minute + "分钟";
        }

    }

    /**
     * 数字格式转换 小于10的数字前补0
     */
    public static String formatNum(int num) {
        if (num < 10) {// 显示x秒
            return "0" + num;
        } else {
            return "" + num;
        }

    }

    /**
     * 格式化日期时间
     *
     * @param time    时间戳
     * @param pattern 时间格式
     */
    public static String formatDateTime(long time, String pattern) {
        SimpleDateFormat mDateFormat = new SimpleDateFormat(pattern);
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    /**
     * 格式化日期时间
     *
     * @param date 20150304
     * @return 2015-03-04
     */
    public static String formatDate(String date) {
        StringBuffer sb = new StringBuffer();
        if (date.length() == 8) {
            sb.append(date.substring(0, 4)).append("-").append(date.substring(4, 6)).append("-").append(date.substring(6, 8));
        }
        return sb.toString();
    }

    /**
     * 格式化金额
     */
    public static String parseMoney(String pattern, BigDecimal bd) {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(bd);
    }

    public static void addEditSpace(CharSequence s, int start, int before, EditText _text) {
        if (s == null || s.length() == 0)
            return;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (i != 3 && i != 8 && s.charAt(i) == ' ') {
                continue;
            } else {
                sb.append(s.charAt(i));
                if ((sb.length() == 4 || sb.length() == 9) && sb.charAt(sb.length() - 1) != ' ') {
                    sb.insert(sb.length() - 1, ' ');
                }
            }
        }
        if (!sb.toString().equals(s.toString())) {
            int index = start + 1;
            if (sb.charAt(start) == ' ') {
                if (before == 0) {
                    index++;
                } else {
                    index--;
                }
            } else {
                if (before == 1) {
                    index--;
                }
            }
            _text.setText(sb.toString());
            _text.setSelection(index);

        }
    }

    /**
     * yyyyMMddHHmmss 转 yyyy/MM/dd HH:mm
     *
     * @param time
     * @return
     */
    public static String changeTime(String time, String timeFormat) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat newFormat = new SimpleDateFormat(timeFormat);
        Date currentTime = new Date();
        try {
            currentTime = oldFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newFormat.format(currentTime);
    }

    /**
     * yyyyMMddHHmmss 转 yyyy/MM/dd HH:mm
     *
     * @param time
     * @return
     */
    public static String changeTime1(String time, String timeFormat) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat newFormat = new SimpleDateFormat(timeFormat);
        Date currentTime = new Date();
        try {
            currentTime = oldFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newFormat.format(currentTime);
    }

    /**
     * String转金额格式
     *
     * @param amount
     * @return
     */
    public static String getMoneyFormat(String amount) {
        BigDecimal bd = new BigDecimal(amount);
        return parseMoney("#,###,##0.00", bd);
    }

}
